package com.yuzhi.master.trunkMain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.util.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import com.yuzhi.master.trunkMain.domain.TrunkMain;
import com.yuzhi.master.trunkMain.domain.bo.TrunkMainBo;
import com.yuzhi.master.trunkMain.domain.request.TrunkMainReq;
import com.yuzhi.master.trunkMain.domain.vo.TrunkMainVo;
import com.yuzhi.master.trunkMain.mapper.TrunkMainMapper;
import com.yuzhi.master.trunkMain.service.ITrunkMainService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.List;

/**
 * 干管表Service业务层处理
 *
 * @author auto code generator
 * @date 2025-08-13
 */
@Validated
@RequiredArgsConstructor
@Service
public class TrunkMainServiceImpl implements ITrunkMainService {

    private final TrunkMainMapper baseMapper;

    /**
     * 查询干管表
     */
    @Override
    public TrunkMainVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询干管表列表
     */
    @Override
    public PageDataInfo<TrunkMainVo> queryPageList(TrunkMainReq request) {
        Page<TrunkMainVo> page = baseMapper.selectPageList(
            new Page<>(request.getPageQuery().getPageNum(), request.getPageQuery().getPageSize()),
            buildQueryWrapper(request)
        );
        return PageDataInfo.build(page);
    }

    /**
     * 查询干管表列表
     */
    @Override
    public List<TrunkMainVo> queryList(TrunkMainReq request) {
        return baseMapper.selectVoList(buildQueryWrapper(request));
    }

    @Override
    public List<TrunkMainVo> queryBachList() {
        return baseMapper.queryBachList();
    }



    private LambdaQueryWrapper<TrunkMain> buildQueryWrapper(TrunkMainReq request) {
        LambdaQueryWrapper<TrunkMain> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(request.getId()), TrunkMain::getId, request.getId());
        lqw.eq(StringUtils.isNotBlank(request.getTrunkmainId()), TrunkMain::getTrunkmainId, request.getTrunkmainId());
        lqw.like(StringUtils.isNotBlank(request.getTrunkmainName()), TrunkMain::getTrunkmainName, request.getTrunkmainName());
        return lqw;
    }

    /**
     * 新增干管表
     */
    @Override
    public Boolean insertByBo(TrunkMainBo bo) {
        TrunkMain add = BeanUtil.toBean(bo, TrunkMain.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改干管表
     */
    @Override
    public Boolean updateByBo(TrunkMainBo bo) {
        TrunkMain update = BeanUtil.toBean(bo, TrunkMain.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TrunkMain entity) {
        // 检查干管代码ID是否重复
        if (checkTrunkmainIdExists(entity.getTrunkmainId(), entity.getId())) {
            throw new RuntimeException("干管代码ID已存在，请使用其他代码");
        }
    }

    /**
     * 根据id删除干管表信息
     */
    @Override
    public Boolean deleteById(String id, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 校验并批量删除干管表信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据干管代码ID查询
     */
    @Override
    public TrunkMainVo queryByTrunkmainId(String trunkmainId) {
        LambdaQueryWrapper<TrunkMain> lqw = Wrappers.lambdaQuery();
        lqw.eq(TrunkMain::getTrunkmainId, trunkmainId);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 根据干管名称模糊查询
     */
    @Override
    public TrunkMainVo queryByTrunkmainName(String trunkmainName) {
        LambdaQueryWrapper<TrunkMain> lqw = Wrappers.lambdaQuery();
        lqw.like(TrunkMain::getTrunkmainName, trunkmainName);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 检查干管代码ID是否已存在
     */
    @Override
    public Boolean checkTrunkmainIdExists(String trunkmainId, String excludeId) {
        LambdaQueryWrapper<TrunkMain> lqw = Wrappers.lambdaQuery();
        lqw.eq(TrunkMain::getTrunkmainId, trunkmainId);
        if (StringUtils.isNotBlank(excludeId)) {
            lqw.ne(TrunkMain::getId, excludeId);
        }
        return baseMapper.selectCount(lqw) > 0;
    }
}
